<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryObj.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="hChange"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          check-strictly
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button
            size="mini"
            type="primary"
            @click="$router.push('/employee/detail')"
          >添加员工</el-button>
          <el-button
            v-allow="'import_employee'"
            size="mini"
            @click="$router.push('/import')"
          >excel导入</el-button>
          <el-button size="mini" @click="hExcel">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="hLook(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗?" @onConfirm="hDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            :current-page="queryObj.page"
            :background="true"
            layout="prev, pager, next, sizes"
            :total="total"
            :page-size="queryObj.pagesize"
            @current-change="hCurrentChage"
            @size-change="hSizeChange"
          />
        </el-row>
        <!-- 分页 -->
      </div>
    </div>
    <import-excel :show-excel-dialog.sync="showExcelDialog" />
    <assign-role ref="refRole" :show-role-dialog.sync="showRoleDialog" :cur-id="curId" />
  </div>
</template>

<script>
import { delEmployee, getEmployeeList } from '@/api/employee'
import { getDepartment } from '@/api/department'
import { arr2tree } from '@/utils/index'
import FileSaver from 'file-saver'
import { exportEmployee } from '@/api/employee'
import ImportExcel from './components/import-excel.vue'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel,
    AssignRole
  },
  data() {
    return {
      curId: -1,
      curEmployeId: '',
      showRoleDialog: false,
      showExcelDialog: false,
      total: 0,
      employList: [],
      depts: [], // 组织数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: null
      },
      userInfo: {
        roles: {
          menus: [], // 菜单权限数据
          points: [] // 按钮权限数据
        }
      }
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    hLook(id) {
      console.log('当前要分配角色id是', id)
      this.showRoleDialog = true
      this.curId = id
      this.$nextTick(() => {
        this.$refs.refRole.getUserRole()
      })
    },
    async hDel(id) {
      const res = await delEmployee(id)
      console.log(res)
      if (this.employList.length === 1 && this.queryObj.page > 1) this.queryObj.page--
      this.loadDepartment()
      this.$message.success('删除员工成功')
    },
    async hExcel() {
      const result = await exportEmployee() // 导出所有的员工接口
      // console.log(result) // 使用一个npm包 直接将blob文件下载到本地 file-saver
      // FileSaver.saveAs(blob对象,文件名称)
      FileSaver.saveAs(result, '员工信息表.xlsx') // 下载文件
    },
    hChange() {
      console.log(this.queryObj.keyword)

      clearTimeout(this.timerId)
      // 更新参数 -- v-model
      this.timerId = setTimeout(() => {
        // 发请求 从第一页开始找
        this.queryObj.page = 1
        this.loadEmployment()
      }, 500)
    },
    async loadEmployment() {
      const res = await getEmployeeList(this.queryObj)
      this.total = res.total
      this.employList = res.rows
    },
    async loadDepartment() {
      const res = await getDepartment()
      const rs = arr2tree(res, 0)
      this.depts = rs
      const id = rs[0].id
      this.queryObj.departmentId = id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(id)
      })
      this.loadEmployment()
    },
    selectNode(data, node) {
      console.log(data, node)
      this.queryObj.departmentId = data.id
      this.queryObj.page = 1
      this.loadEmployment()
    },
    changePage(newPage) {
      this.queryObj.page = newPage // 赋值新页码
      this.getEmployeeList() // 查询数据
    },
    hSizeChange(pages) {
      console.log(pages)
      // 1. 更新每页显示的条数
      this.queryObj.pagesize = pages

      // 2. 更新页码为1
      this.queryObj.page = 1
      // 3. 重发请求
      this.loadDepartment()
    },
    // page: 当前用户选择的页数
    hCurrentChage(page) {
      // 1. 更新页码
      this.queryObj.page = page
      // 2. 重发请求
      this.loadDepartment()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
